
public class CheckOrderAdvanced 
{

	public static void main(String[] args) 
	{
		int [] myArray1 = {-2,5,54,4,2,1,5,4,87,-754,45,98,21,9944,56464}; //create an array for testing
		System.out.println("the value for myArray1 before sorting is 	"); 
		printout(myArray1);
		
		sort(myArray1);
		
		System.out.println("the value for myArray1 after sorting is 	"); 
		printout(myArray1);
	}
	
	
	static  void printout(int [] input) // this is the function for printing array
	{
		for (int i = 0;i<input.length;i++)
			System.out.print(input[i]+"  ");
		
		System.out.println();
		System.out.println();
	}
	
	static void sort(int []  array_input)
	{
		int[] previous_array_input = new int [array_input.length];
		boolean previous_array_and_new_array_is_the_same = false;

		 int counter = 0; //just for counting total repeat counts
		 
		 while(!previous_array_and_new_array_is_the_same) //when old and new array is not the same, keep sorting
			{
				previous_array_input=createDuplicate(array_input); //create a duplicate array to compare later if the new array and old array have to same value
				
				for (int i = 0;i< array_input.length -1 ;i++) // go through all array and check the highest
				{
					if (array_input[i]>array_input[i+1])
					{
					swap(array_input,i,i+1); //if, incorrectly ordered, then swap
					}
				}
				
				previous_array_and_new_array_is_the_same=IsArrayEqual(previous_array_input,array_input);// get result of whether the old and new array is the same
				
				counter++; //increase number of total sorting done
			}
		System.out.println("There are a total of "+array_input.length+"	arrays");
		System.out.println("The sorting has been repeated "+counter+" times \n");
	}
	
	static void swap(int[] array, int index1, int index2)
	//swaps the items at location array[index1] and array[index2]
	{
		int temp = array[index1];
		array[index1]=array[index2];
		array[index2]=temp;
	}
	
	static boolean IsArrayEqual(int [] input1, int [] input2)
	{
		// check if array input1 & array input2 is the same;
		boolean result = false;
		
		for (int k = 0,point=0;k<input1.length;k++)
		{
			if (input1[k] ==input2[k])
			{
				point++;
			}
			
			if (point==input1.length)
			{
				result = true;
			}
		}
		return result;
	}

	static int[] createDuplicate(int [] array__input)
	{
		// this function create a duplicate of an array
		int [] duplicate_array = new int[array__input.length];
		
		for (int i = 0;i <duplicate_array.length;i++)
		{
			duplicate_array[i] = array__input[i];
		}
		
		return duplicate_array;
	}
}
